﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Xml.Linq;

namespace AG.Data
{
    public class AGResourceManager
    {
        private Dictionary<string, AGTexture> _modelTextureList;

        private Dictionary<ushort, AGModel> _modelList;

        private string _dataFolder;

        public AGResourceManager(string dataFolder)
        {
            _dataFolder = dataFolder;
            _modelTextureList = new Dictionary<string, AGTexture>();
            _modelList = new Dictionary<ushort, AGModel>();
        }

        public AGModel GetModel(ushort modelId)
        {
            if (_modelList.ContainsKey(modelId))
            {
                return _modelList[modelId];
            }
            string modelFile = string.Format("{0}\\Models\\{1}.xml", _dataFolder, modelId);
            XDocument xDoc = XDocument.Load(modelFile);
            XElement xModel = xDoc.Element("model");

            AGModel model = new AGModel();
            model.Id = (ushort)Convert.ToInt16(xModel.Attribute("id").Value);
            model.Caption = xModel.Attribute("caption").Value;
            //model.CategoryId = (ushort)Convert.ToInt16(xModel.Attribute("category-id").Value);

            List<XElement> xActionList = xModel.Elements("action").ToList();
            for (int actionIndex = 0; actionIndex < xActionList.Count; actionIndex++)
            {
                XElement xAction = xActionList[actionIndex];
                AGModelAction modelAction = new AGModelAction();
                model.Actions.Add(modelAction);
                modelAction.Id = (byte)Convert.ToInt32(xAction.Attribute("id").Value);

                List<XElement> xDirectionList = xAction.Elements("direction").ToList();
                for (int dirIndex = 0; dirIndex < xDirectionList.Count; dirIndex++)
                {
                    XElement xDirection = xDirectionList[dirIndex];
                    AGModelDirection modelDirection = new AGModelDirection();
                    modelAction.Directions.Add(modelDirection);
                    modelDirection.Id = Convert.ToByte(xDirection.Attribute("id").Value);

                    List<XElement> xFrameList = xDirection.Elements("frame").ToList();
                    for (int frameIndex = 0; frameIndex < xFrameList.Count; frameIndex++)
                    {
                        XElement xFrame = xFrameList[frameIndex];
                        AGModelFrame modelFrame = new AGModelFrame();
                        modelDirection.Frames.Add(modelFrame);

                        modelFrame.Id = Convert.ToByte(xFrame.Attribute("id").Value);
                        string strPivot = xFrame.Attribute("pivot").Value;
                        string[] strPivotArr = strPivot.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                        string strRect = xFrame.Attribute("rect").Value;
                        string[] strRectArr = strRect.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                        modelFrame.Pivot = new Point(Convert.ToInt32(strPivotArr[0]), Convert.ToInt32(strPivotArr[1]));
                        modelFrame.SrcRect = new Rectangle(Convert.ToInt32(strRectArr[0]), Convert.ToInt32(strRectArr[1]),
                            Convert.ToInt32(strRectArr[2]) - Convert.ToInt32(strRectArr[0]),
                            Convert.ToInt32(strRectArr[3]) - Convert.ToInt32(strRectArr[1]));
                        modelFrame.Texture = string.Format("Models\\{0}\\{1}", model.Id, xFrame.Attribute("texture").Value.Replace(".png", string.Empty));
                    }
                }
            }
            return model;
        }

        public AGTexture GetTexture(string name)
        {
            string keyName = name; 
            if (name.EndsWith(".png"))
            {
                keyName = keyName.Replace(".png", string.Empty);
            }

            if (_modelTextureList.ContainsKey(keyName))
            {
                return _modelTextureList[keyName];
            }

            string textureFullName = string.Format("{0}\\{1}.png", _dataFolder, keyName);
            
            Image bitmap = new Bitmap(textureFullName);
            AGTexture texture = new AGTexture();
            texture.Bitmap = bitmap;
            texture.Size = new Size(bitmap.Width, bitmap.Height);

            _modelTextureList.Add(keyName, texture);
            return texture;
        }
    }
}
